stream To Ui Messages
Convert a raw agent StreamEvent flow into a flow of growing UIMessage snapshots. Each emission is the current full state of the assistant message being built — caller's StateFlow<List<UIMessage>> just replaces the in-flight message on each emission.
Hosts use this to convert raw stream events into renderable chat snapshots. The v6 equivalent is the useChat hook's internal aggregation; here we expose it as a pure transformation.
Block-aware: the v6 stream taxonomy uses block IDs to interleave multiple text segments / reasoning blocks / tool inputs in a single step. Each *Start opens a part by ID; each *Delta grows it; each *End closes it. Multiple text or reasoning blocks in one message become separate UIMessageParts in render order.
Usage:
val messages: StateFlow<List<UIMessage>> = chatRepository
.streamReply(turns)
.let { streamToUiMessages(it, assistantMessageId = "asst_${turns.size}") }
.stateIn(scope, SharingStarted.Eagerly, emptyList())Content copied to clipboard